package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.ControllerBean;
import com.qiantu.api.entity.KGXAddData;
import com.qiantu.api.entity.KGXcollectionData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.ui.activity.ControllerDeviceAcitvity;
import com.qiantu.phone.ui.activity.ControllerSettingActivity;
import com.qiantu.phone.ui.activity.ControllerTLPKGXActivity;
import com.qiantu.phone.ui.activity.KGXAddActivity;
import com.qiantu.phone.ui.activity.KGXControllerDeviceAcitvity;
import com.qiantu.phone.ui.activity.KGXControllerSettingActivity;
import com.qiantu.phone.ui.activity.LLRegionalGatewayActivity;

public class TLPKGXAdapter extends AppAdapter<KGXcollectionData> {
    public TLPKGXAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public TLPKGXAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TLPKGXAdapter.ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private final ImageView iconController;
        private final TextView tvControllerName;
        private final TextView tvControllerId;
        private final ImageView iconControllerStatus;
        private final TextView tvControllerStatus;
        private final LinearLayout allCell;
        private final LinearLayout addNew;

        private ViewHolder() {
            super(R.layout.item_tlpkgx_controller);
            iconController = findViewById(R.id.icon_controller);
            tvControllerName = findViewById(R.id.tv_controller_name);
            tvControllerId = findViewById(R.id.tv_controller_id);
            iconControllerStatus = findViewById(R.id.icon_controller_status);
            tvControllerStatus = findViewById(R.id.tv_controller_status);
            allCell = findViewById(R.id.showCell);
            addNew = findViewById(R.id.addNew);
//            itemView.setOnClickListener(mOnClickListener);
            tvControllerStatus.setVisibility(View.GONE);
        }

        @Override
        public void onBindView(int position) {
            KGXcollectionData item = getItem(position);
            String controllerSerialNo = item.getControllerSerialNo();
            if (controllerSerialNo == null || controllerSerialNo.isEmpty()) {
                allCell.setVisibility(View.GONE);
                addNew.setVisibility(View.VISIBLE);
            } else {
                allCell.setVisibility(View.VISIBLE);
                addNew.setVisibility(View.GONE);
            }
            itemView.setTag(item);
            GlideApp.with(iconController).load(item.getImageUrl()).into(iconController);
            tvControllerName.setText(item.getName());
            String protocolAddress = item.getProtocolAddress();
            tvControllerId.setText("地址: " + (protocolAddress != null ? protocolAddress : "未设置"));

        }
    }

//    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
//        @Override
//        public void onClick(View v) {
//            KGXcollectionData item = (KGXcollectionData)v.getTag();
//            if (item.getControllerSerialNo() == null){
//                KGXAddActivity.start(getContext(), item.getControllerSerialNo().toString());
//            }else {
//                if (item.getType() == 22){//弱电旋钮空调面板
//                    KGXControllerSettingActivity.start(getContext(), item.getControllerSerialNo().toString(),item.getDeviceCollectSerialNo().toString(),item.getType(),item.getFromPos(),item.getName().toString(),item.getImageUrl().toString(),item.getProtocolAddress().toString());
//                }else {
//                    KGXControllerDeviceAcitvity.start(getContext(), item.getControllerSerialNo().toString(),item.getDeviceCollectSerialNo().toString());
//                }
//            }
//        }
//    };
}
